package com.example.demo.controller.front;

import com.example.demo.dao.*;

import com.example.demo.service.AddressService;
import com.example.demo.service.GoodsService;
import com.example.demo.service.UserService;
import com.example.demo.service.indentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
//@RequestMapping(value = "/use")
public class userMsg {
    @Autowired
    private indentService indentService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private AddressService addressService;
    @RequestMapping("/userindes")
    public String userindex(HttpSession session, Model model){
        Integer id =Integer.parseInt(session.getAttribute("id").toString()) ;
        List<Indent> indentList = indentService.findIndentByUser(id);
        List<Indent> indents=new ArrayList<>();
//        Goods goods = null;
        for(Indent indent: indentList){
//           goods= goodsService.findGoodsByPrimaryKey(indent.getGoodsid());
           /*goodsList.add(goods);
//            List<Goods>  goods=goodsService.findgoods(indentList.get(i).getGoodsid());
            indent.setGoodsList(goodsList);*/
           if(indent.getIsreceive()==0){
               indents.add(indent);
           }
        }
//        model.addAttribute("goods",goods.getPath());
        model.addAttribute("size",indents.size());
        model.addAttribute("indentList",indents);

        return "/index-user";
    }
//    地址查询
    @RequestMapping("/address")
    public String address(HttpSession session,Model model){
        Integer id=(Integer)session.getAttribute("id");
        List<Address> address= addressService.findaddById(id);
        model.addAttribute("addresses",address);
        return "/address";
    }
    //删除地址
    @RequestMapping("/deladdr")
    @ResponseBody
    private void deladdr(@RequestParam("id")Integer id){
        addressService.deladdByAddressid(id);
    }
//    增加新地址
    @RequestMapping("/addAddress")
    private String addAddress(@RequestParam("conname")String conname,@RequestParam("contel")String contel,@RequestParam("province")String province,@RequestParam("city")String city,@RequestParam("county")String county,@RequestParam("detailaddr")String detailaddr,HttpSession session){
        System.out.println(addressService.findMaxAddressid());
        Address address = new Address(addressService.findMaxAddressid()+1,(Integer) session.getAttribute("id"),province,city,county,detailaddr,conname,contel);
//        String id = UUID.randomUUID().toString();
//        int addressId = Integer.parseInt(id.substring(id.length()-4,id.length()));
        /*address.setAddressid(1);
        address.setUserid((Integer) session.getAttribute("id"));
        address.setCity(city);
        address.setConname(conname);
        address.setContel(contel);
        address.setDetailaddr(detailaddr);
        address.setProvince(province);
        address.setCounty(county);*/
        addressService.insert(address);
        return "/address";
       // return "redirect:/userindes";
    }
    //信息
    @RequestMapping("/informations")
    public String information(){
        return "/information";
    }
    //确认收货
    @RequestMapping("/receive")
    @ResponseBody
    private Boolean Receive(@RequestParam("id")Integer id){
        Indent indent = indentService.findIndentById(id);
        indent.setIsreceive(Byte.parseByte("1"));
        int updated  = indentService.updateIndent(indent);
        return updated>0;
    }
    @RequestMapping("/safety")
    public String safety(){
        return "/safety";
    }
    @RequestMapping("/users/alter")
    @ResponseBody
    public String useralter(@RequestParam("name")String name,@RequestParam("tel")String tel,@RequestParam("email")String email,HttpSession session, Model model){
        Integer id =(Integer)session.getAttribute("id");
        User user=userService.findUser(id);
        user.setUsername(name);
        user.setTelephone(tel);
        user.setEmail(email);
        userService.editUser(user);
        return "ok";
    }
    @RequestMapping("/passwordAlter")
    public String passwordAlter(){

        return "password";
    }
    //判断密码是否和后台密码一样
    @RequestMapping("/passTrueOrFalse")
    @ResponseBody
    public boolean pass(@RequestParam("password") String password,HttpSession session){
        User user=userService.findUser((Integer)session.getAttribute("id"));
        if(password.equals(user.getPassword())){
            return true;
        }
        else {
            return false;
        }
    }
    //提交密码
    @PostMapping(value="/passwordsubmit")
    public String passwordsubmit(@RequestParam("password")String password,HttpSession session){
        User user=userService.findUser((Integer)session.getAttribute("id"));
        user.setPassword(password);
        userService.editUser(user);
        return "redirect:/users/login";
    }
}
